package com.cn.jinl.service;

import com.cn.jinl.api.IFileInfoService;
import com.cn.jinl.api.IFrontDisplayService;
import com.cn.jinl.dao.FrontDisplayMapper;
import com.cn.jinl.domain.FileInfo;
import com.cn.jinl.domain.FrontDisplay;
import com.cn.jinl.domain.FrontDisplayExample;
import com.cn.jinl.domain.FrontDisplayVo;
import com.cn.jinl.utils.CommonUtil;
import com.cn.jinl.utils.QueryForm;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Created On : 2024/1/12.
 * <p>
 * Author : Huiyan
 * <p>
 * Description: FrontDisplayServiceImpl
 */
@Service
public class FrontDisplayServiceImpl implements IFrontDisplayService {

    @Autowired
    private FrontDisplayMapper frontDisplayMapper;

    @Autowired
    private IFileInfoService fileInfoService;


    private static Object lock = new Object();

    @Override
    public FrontDisplayVo getFrontDisplay(QueryForm queryForm) throws Exception {
        List<FrontDisplay> frontDisplays = frontDisplayMapper.selectFrontDisplays(queryForm);
        List<FileInfo> fileInfos = fileInfoService.queryFileList(queryForm);
        FrontDisplayVo frontDisplayVo = new FrontDisplayVo();
        frontDisplayVo.setFileInfos(fileInfos);
        frontDisplayVo.setFrontDisplays(frontDisplays);
        return frontDisplayVo;
    }

    @Override
    public String addOrUpdateFrontDisplay(FrontDisplayVo frontDisplayVo) throws Exception {
        List<FrontDisplay> frontDisplays = frontDisplayVo.getFrontDisplays();
        List<FileInfo> fileInfos = frontDisplayVo.getFileInfos();
        if(CollectionUtils.isEmpty(frontDisplays)){
            return "参数异常";
        }
        String pageAttr =frontDisplayVo.getPageAttr();
        if(StringUtils.isEmpty(pageAttr)){
            return "参数缺少页面属性";
        }
        FrontDisplayExample example = new FrontDisplayExample();
        example.createCriteria().andPageAttrEqualTo(pageAttr);
        int count = 0;
        synchronized (lock){
            count = frontDisplayMapper.deleteByExample(example);
            if(count < 0){
                return "删除旧数据异常！";
            }
            String result = fileInfoService.deleteFileByType(pageAttr);
            if(StringUtils.isNotEmpty(result)){
                throw new Exception("删除文件异常");
            }

            for(FrontDisplay frontDisplay:frontDisplays){
                frontDisplay.setId(CommonUtil.generateRandomNum("front-"));
            }
            count = frontDisplayMapper.batchInsertFrontDisplay(frontDisplays);
            if(count < 1){
                throw new Exception("插入数据异常");
            }
            if(CollectionUtils.isEmpty(fileInfos)){
                return "";
            }
            result = fileInfoService.batchAddFile(fileInfos);
            if(StringUtils.isNotEmpty(result)){
                throw new Exception("插入文件异常");
            }
        }
        return "";
    }

    @Override
    public String addFrontDisplay(FrontDisplay frontDisplay) throws Exception {
        FrontDisplayExample example = new FrontDisplayExample();
        example.createCriteria().andPageTypeEqualTo(frontDisplay.getPageType()).andPageAttrEqualTo(frontDisplay.getPageAttr());
        if(frontDisplayMapper.countByExample(example) > 10){
            return "最多添加10条记录！";
        }
        String id = CommonUtil.generateRandomNum("front-");
        frontDisplay.setId(id);
        int count = frontDisplayMapper.insertSelective(frontDisplay);
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateFrontDisplay(FrontDisplay frontDisplay) throws Exception {
        String id = frontDisplay.getId();
        if(StringUtils.isEmpty(id)){
            return "缺少id！";
        }
        int count = frontDisplayMapper.updateByPrimaryKeySelective(frontDisplay);
        return CommonUtil.outStr(count);
    }

    @Override
    public String deleteFrontDisplay(String id) throws Exception {
        if(StringUtils.isEmpty(id)){
            return "缺少id！";
        }
        int count = frontDisplayMapper.deleteByPrimaryKey(id);
        return CommonUtil.outStr(count);
    }
}
